<?php 
Class order extends Load_use
{
	private $product = '';
	private $row_proposal_html = '';
	private $row_price_html = '';
	private $arrow_html = '';
	private $row_total_price_html = '';
	
	function getBlock()
	{
		$list = '';
		
		$basket_id = getBasketId();
		$products = getBasketProducts( $basket_id );
		if( !$products  && !stripos($_SERVER['HTTP_REFERER'],'yandex'))
		{ 
			header("Location: http://".$_SERVER['HTTP_HOST']);
			exit;
		}
		
		$costs = getTotalCosts( $basket_id, 'courier' );

		
		foreach( $products['order_list'] as $this->product )
		{
			$this->getPanelBuying();
			
			//Выводит подарок у товара
			if (array_key_exists($this->product->product_code, $products['presents'])) {
				$present_name = $products['presents'][$this->product->product_code]['name'];
				$present_url = $products['presents'][$this->product->product_code]['url'];
			}
			else
				$present_name = $present_url = '';
				
			if( $present_name && $present_url ) {
				$present_html = '
				<div class="present">
					<div class="present_inner">
						<span class="present_box"></span>
						<div class="present_text">
							<div class="present_text_inner">
								При покупке <a target="_blank" href="'.$present_url.'">'.$present_name.'</a> бесплатно, в подарок!
								<span class="present_text_triangle"></span>
							</div>
						</div>
					</div>
				</div>';
			}
			else
				$present_html = '';
				
			//Выводит бонусы новогодней акции
			if( array_key_exists($this->product->product_code, $products['credits']) ) {
				$product_credit = $products['credits'][$this->product->product_code].'<br/>';
			}
			else
				$product_credit = '';
				
			if( $product_credit ) {
				$product_credit_html = '<a href="/bonusnaya-akciya" title="Новогодняя акция" class="ny_product_bottom">'.$product_credit.'</a>';
			}
			else
				$product_credit_html = '';

			$list .= '
					<li class="orders__products" product_code="'.$this->product->product_code.'">
						<ul>
							<li class="selected-products__item">
								<div class="global-photo">
									<a href="'.$this->product->url.'" target="_blank">
										<img src="'.getImageSrcNew( $this->product->product_code , $size = 'k' ).'" alt="" />
									</a>
									'.$product_credit_html.'
									'.$present_html.'
								</div>
								<div class="selected-products__item__description">
									<span class="selected-products__name">Нименование:</span>
									<div class="selected-products__title">
										<h2><a href="'.$this->product->url.'" target="_blank">'.$this->product->prefix.' '.$this->product->brand.' '.$this->product->name.'</a></h2><br/>
										<span class="selected-products__article">(Артикул: '.$this->product->product_code.')</span>
									</div>
									'.$this->row_proposal_html.'
								</div>
							</li>
							<li class="selected-products__item price">
								<dl>
									<dt>Цена:</dt>
									<dd class="first">'.getPriceFormat( $this->product->price_retail ).' руб.</dd>
									'.$this->row_price_html.'
								</dl>
							</li>
							<li class="selected-products__item number">
								<dl>
									<dt>Кол-во:</dt>
									<dd>
										<ul class="number-navi small" id="product">
											<li class="number-navi__less arrow">
												<span>-</span>
											</li>
											<li>
												<input type="text" class="select_count product" value="'.$this->product->count.'" />
											</li>
											<li class="number-navi__more arrow">
												<span>+</span>
											</li>
										</ul>
										'.$this->arrow_html.'
									</dd>
								</dl>
							</li>
							<li class="selected-products__item price">
								<dl>
									<dt>Стоимость:</dt>
									<dd class="first product_total_price"><span>'.getPriceFormat( $this->product->price_retail * $this->product->count ).'</span> руб.</dd>
									'.$this->row_total_price_html.'
								</dl>
							</li>
							<li class="selected-products__item delete">
								<a href="#" class="selected-products__item__delete-icon">x</a>
								<div class="selected-products__item__delete-description">
									<p>Удалить из заказа</p>
								</div>
							</li>
						</ul>	
					</li>';
		}

		
		$user = (object)array(
			'name'=>'',
			'phone'=>'',
			'email'=>'',
			'addres'=>'',
		);

		if(!empty($_POST) && stripos($_SERVER['HTTP_REFERER'],'yandex'))
		{
			echo 'POST';
			$user->email = $_POST['email'];
			$user->phone = $_POST['phone'];
			$user->name = $_POST['firstname'].' '.$_POST['lastname'].$_POST['fathersname'];

			$user->addres = $_POST['country'];

			if(!empty($_POST['city']))
				$user->addres.=' г.'.$_POST['city'];

			if(!empty($_POST['metro']))
				$user->addres.=' метро:'.$_POST['metro'];

			if(!empty($_POST['street']))
				$user->addres.=' ул.'.$_POST['street'];

			if(!empty($_POST['building']))
				$user->addres.=' дом:'.$_POST['building'];

			if(!empty($_POST['entrance']))
				$user->addres.=' подъзд:'.$_POST['entrance'];

			if(!empty($_POST['floor']))
				$user->addres.=' этаж:'.$_POST['floor'];

			if(!empty($_POST['flat']))
				$user->addres.=' кв.'.$_POST['flat'];
		}


		return '<div class="basket_block">
					<ul class="selected-products">'.$list.'</ul>
					<form action="post" class="ordering_form">
						<div class="delivery-method">
							<dl class="delivery-method__choice">
								<dt>Способ доставки:</dt>
								<dd>
									<ul>
										<li>
											<div class="type-radio">
												<input type="radio" id="delivery_type_courier" name="order[delivery_type]" value="courier" checked="checked"/>
												<label for="delivery_type_courier"><span>&nbsp;</span> Курьер</label>
											</div>
											<p>Стоимость доставки: <span class="delivery_price_courier">'.getPriceFormat( $costs['courier'] ).'</span> руб.</p>
										</li>
										<li>
											<div class="type-radio">
												<input type="radio" id="delivery_type_self" name="order[delivery_type]" value="self"/>
												<label for="delivery_type_self"><span>&nbsp;</span>	Самовывоз</label>
											</div>
											<p>Стоимость самовывоза: <span class="delivery_price_self">'.getPriceFormat( $costs['self'] ).'</span> руб.</p>
										</li>
									</ul>
								</dd>
							</dl>
							<dl class="delivery-method__total">
								<dt>Итого:</dt>
								<dd class="total_price">
									<span>'.getPriceFormat( $costs['total'] ).'</span> руб.
								</dd>
							</dl>
						</div>
						<div class="registration-order">
							<div class="registration-order__form">
								<fieldset>
									<h3 class="middle-title">Заказчик</h3>
									<ul class="registration-order__form__user-info">
										<li>
											<a href="http://market.yandex.ru/addresses.xml?callback=http%3A%2F%2Fraybt.ru%2Forder"><img src="http://cards2.yandex.net/hlp-get/5814/png/3.png" border="0" /></a>
										</li>
										<li>
											<label for="name">Ф.И.О. *</label>
											<input id="name" name="order[name]" class="type-text" type="text" value="'.$user->name.'"/>
										</li>
										<li>
											<label for="phone">Номер телефона *</label>
											<input id="phone" name="order[phone]" class="type-text" type="text"  value="'.$user->phone.'"/>
										</li>	
										<li>
											<label for="e-mail">Электронная почта *</label>
											<input id="e-mail" name="order[email]" class="type-text" type="text"  value="'.$user->email.'"/>
										</li>
										<li id="delivery_block">
											<label for="delivery_address">Адрес доставки</label>
											<textarea id="delivery_address" name="order[adress]" cols="45" rows="3">'.$user->addres.'</textarea>
										</li>
									</ul>
								</fieldset>
								<fieldset><input type="submit" class="big-button" value="Подтвердить заказ" id="ordering_button"/></fieldset>
							</div>
						</div>
					</form>
				</div>
				<div class="submit_form_loader_main">'.$this->load->block('submit_form_loader').'</div>';

	}
	
	function getPanelBuying()
	{
		$proposal = $proposal_inst_html = $proposal_vip_html = '';
		$price_inst_html = $vip_price_html = '';
		$count_inst_html = $count_vip_html = '';
		$total_price_inst_html = $total_price_vip_html = '';
		

		if( strlen( $this->product->price_inst ) > 0 ) 
		{
			$proposal_inst_html = '	<li>
										<div class="type-radio ">
											<input type="checkbox" id="installation_'.$this->product->product_code.'" name="services" class="installation_in_order" '.( $this->product->inst ? 'checked="checked"' : '' ).'/>
											<label for="installation_'.$this->product->product_code.'"><span>&nbsp;</span> Купить с монтажом</label>
										</div>	
									</li>';
			$price_inst_html = '<dd class="small_size">'.( $this->product->price_inst != 0 ? getPriceFormat( $this->product->price_inst ).' руб.' : 'Бесплатно'  ).'</dd>';
			$count_inst_html = '<span class="selected-products__number installation">'.$this->product->count_inst.'</span>';
			$total_price_inst_html = '<dd class="small_size inst_total_price'.( $this->product->inst ? '' : ' disabled' ).'"><span>'.getPriceFormat( $this->product->price_inst * $this->product->count_inst ).'</span> руб.</dd>';
		}
		
		if( strlen( $this->product->price_vip ) > 0 )
		{
			$proposal_vip_html = '	<li>
										<div class="type-radio">
											<input type="checkbox" id="vip_service_'.$this->product->product_code.'" name="services" class="vip_service_in_order" '.( $this->product->vip ? 'checked="checked"' : '' ).'/>
											<label for="vip_service_'.$this->product->product_code.'"><span>&nbsp;</span> Купить VIP сервис</label>
										</div>	
									</li>';
			$vip_price_html = '<dd class="small_size">'.( $this->product->price_vip != 0 ? getPriceFormat( $this->product->price_vip ).' руб.' : 'Бесплатно'  ).'</dd>';
			$count_vip_html = '<span class="selected-products__number vip_service">'.$this->product->count_vip.'</span>';
			$total_price_vip_html = '<dd class="small_size vip_total_price'.( $this->product->vip ? '' : ' disabled' ).'"><span>'.getPriceFormat( $this->product->price_vip * $this->product->count_vip ).'</span> руб.</dd>';
		}
		
		$proposal = $proposal_inst_html.$proposal_vip_html;
		$this->row_price_html = $price_inst_html.$vip_price_html;
		$this->arrow_html = $count_inst_html.$count_vip_html;
		$this->row_proposal_html = $proposal ? '<ul class="proposal-list">'.$proposal.'</ul>': '';
		$this->row_total_price_html = $total_price_inst_html.$total_price_vip_html;
		
	}
	
}